<?php
/**
 * File keep Llib_List class
 *
 * @author Yaroslav Olekh
 */


/**
 * Llib_List class
 *
 */
class Llib_Files
{
	private $_root;
	private $ERROR;
	
	public  $level_access = 4; // 

	function __construct($root)
	{
		$this->_root = $root;
	}

	function GetError()
	{
		return $this->ERROR;
	}
	
	function SetRootPath($path)
	{
		$this->_root = $path;
	}

	function CreateFolder($name, $path="")
	{
	  $path = trim($path);
	  $name = trim($name);

	  $sep = substr($path, strlen($path)-1, 1);

	  if($sep != '/') $sep = '/';
	  else $sep = '';

	  if(is_dir($this->_root.$path.$sep.$name)) return true;

	  if(!is_dir($this->_root.$path))
	  {
		  if(!mkdir($this->_root.$path, 0777))
		  {
		      $this->ERROR = "Cannot create the folder: ".$path.$name;
		      return false;
		  }
	  }

	  if(!mkdir($this->_root.$path.$sep.$name, 0777))
	  {
	      $this->ERROR = "Cannot create the folder: ".$path.$sep.$name;
	      return false;
	  }

	  return true;
	}

	function RemoveFolder($path)
	{

	  if(!$this->check_path($path)) return false;
	  
	  if(!is_dir($this->_root.$path))
	  {
	      $this->ERROR = "RemoveFolder: Cannot find the path: ".$path;
	      return false;
	  }

	  if($dir = @opendir($this->_root.$path))
	  {
	     while (($file = readdir($dir)) !== false)
	       if ($file != "." && $file != "..")
	       {
	       	  if(is_dir($this->_root.$path."/".$file))
	          {
	          	if(!$this->RemoveFolder($path."/".$file)) return false;
	            continue;
	          }

	          if(!unlink($this->_root.$path."/".$file))
	          {
	             $this->ERROR = "RemoveFolder: Cannot remove file : ".$this->_root.$path."/".$file;
	             if($this->debug) echo $this->ERROR;
	          }
	       }
	  }
	  else
	  {
	     $this->ERROR = "RemoveFolder: Dir is failed : ".$this->_root.$path;
	     return false;
	  }

	  if(!rmdir($this->_root.$path))
	  {
	     $this->ERROR = "RemoveFolder: Cannot remove dir :".$this->_root.$path;
	     return false;
	  }

	  return true;
	}
	

	function EmptyFolder($path)
	{
	  if(!$this->check_path($path)) return false;

	  if(!is_dir($this->_root.$path))
	  {
	      $this->ERROR = "RemoveFolder: Cannot find the path: ".$this->_root.$path;
	      return false;
	  }

	  if($dir = @opendir($this->_root.$path))
	  {
	     while (($file = readdir($dir)) !== false)
	       if ($file != "." && $file != "..")
	       {
	       	  if(is_dir($this->_root.$path."/".$file))
	          {
	          	if(!$this->RemoveFolder($path."/".$file)) return false;
	            continue;
	          }

	          if(!unlink($this->_root.$path."/".$file))
	          {
	             $this->ERROR = "RemoveFolder: Cannot remove file : ".$this->_root.$path."/".$file;
	             if($this->debug) echo $this->ERROR;
	          }
	       }
	  }
	  else
	  {
	     $this->ERROR = "RemoveFolder: Dir is failed : ".$this->_root.$path;
	     return false;
	  }

	  return true;
	}	

	function RemoveFile($path)
	{
      if(!is_file($this->_root.$path)) return true;
	  
	  if(!unlink($this->_root.$path))
      {
         $this->ERROR = "RemoveFile: Cannot remove file : ".$this->_root.$path;
         if($this->debug) echo $this->ERROR;
      }

	  return true;
	}

	function GetImages($path)
	{
	  $data = array();

	  if ($dir = @opendir($this->_root.$path))
	  {
	     $i = 0;
	     while (($file = readdir($dir)) !== false)
	     {
	       $file_path = $this->_root.$path."/".$file;
	       if ($file != "." && $file != ".." && exif_imagetype($file_path) == 2){
	         $data[$i]["name"] = $file;
	         $data[$i]["path"] = "/".$path."/".$file;
	         $data[$i]["path_preview"] = "/".$path."/mcith/mcith_".$file;
	         $data[$i]["size"] = round(filesize($file_path) / 1024, 2);
	         $img_arr = getimagesize($file_path);
	         $data[$i]["h"] = $img_arr[0];
	         $data[$i]["w"] = $img_arr[1];
	         $i++;
	       }
	     }
	  }
	  else
	  {
	    $this->ERROR =  "GetFiles: Dir is failed : ".$this->_root.$path;
	    return false;
	  }
	  closedir($dir);

	  return $data;
	}
	
	
	function check_path($path)
	{
		$path_arr = explode('/', $path);
	  	$path_level = count($path_arr);
		
		 if(!($path_level >= $this->level_access))
		 {
		  	 $this->ERROR = "Cannot empty/remove folder with level ".$path_level;  
		  	 return false;	
		 }
		 
		 foreach($path_arr as $k => $v)		
		 if(trim($v) == '' && $k > 0)
		 {
		  	 $this->ERROR = "Dir name cannot be empty: ".$path;  
		  	 return false;			 	
		 }		 	
		 
		
		 return true;
	}	
}